package com.axend.lib_base.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class RectangleView extends View {

    private Paint paint;
    private int centerX;
    private int centerY;
    private int width;
    private int height;
    private float scale;

    public RectangleView(Context context,int color) {
        super(context);
        init(color);
    }

    public RectangleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(Color.GREEN);
    }

    public RectangleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(Color.GREEN);
    }

    private void init(int color) {
        paint = new Paint();
        paint.setColor(color); // 设置矩形的颜色
        paint.setStyle(Paint.Style.STROKE); // 设置为描边样式
        paint.setStrokeWidth(2.5f); // 设置描边宽度
//        paint.setAntiAlias(true); // 抗锯齿
    }

    public void setRectangleProperties(int centerX, int centerY, int width, int height, float scale) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.width = width;
        this.height = height;
        this.scale = scale;
        invalidate(); // 重新绘制
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int scaledCenterX = (int) (centerX * scale);
        int scaledCenterY = (int) (centerY * scale);
        int scaledWidth = (int) (width * scale);
        int scaledHeight = (int) (height * scale);

        // 计算矩形的左上角和右下角坐标
        int left = scaledCenterX - scaledWidth / 2;
        int top = scaledCenterY - scaledHeight / 2;
        int right = scaledCenterX + scaledWidth / 2;
        int bottom = scaledCenterY + scaledHeight / 2;

        // 绘制矩形区域
        canvas.drawRect(left, top, right, bottom, paint);
    }
}

